home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGMISC / OVL312.LZH / OVL312.ARC / MAPOVL.C < prev    next >
Text File  |  1989-03-19  |  5KB  |  149 lines

  1. /* MAPOVL.C
  2.  
  3.     programmed in the small memory model of Turbo C version 2.0
  4.  
  5.     Show file information for overlays appended to a root .EXE file
  6.  
  7. */
  8.  
  9. #include "stdio.h"
  10. #include "dos.h"
  11. #include "string.h"
  12. #include "io.h"
  13. #include "conio.h"
  14. #include "stdlib.h"
  15.  
  16. FILE *fp;
  17.  
  18. char fname[128];
  19. char f_noext_name[125];    /* file name in fname without extension */
  20. unsigned char header[27];
  21. char buff[8192];    /* file buffer for setvbuf */
  22.  
  23. unsigned int filecount=0;
  24. unsigned long filelen;    /* length of .exe file (overlay or root) */
  25. unsigned long len_no_hdr;    /* length of file without .exe header byte */
  26. unsigned int relocation_count;    /* number of relocation table items */
  27. unsigned long relocation_bytes;    /* byte count of relocation table items */
  28. unsigned int header_size;    /* size of .exe header */
  29. unsigned long ovl_largest=0L;    /* size of largest overlay file */
  30. unsigned int which_ovl=0;    /* overlay file number that is largest */
  31.  
  32. void operate(),report();
  33. int get_info();
  34.  
  35. void main(int argc, char *argv[])
  36. {
  37.     struct date today;
  38.     struct time now;
  39.     char *period_pos;    /* position of period in fname */
  40.     char temp[180];
  41.  
  42.     if(argc<2){    /* file name was not provided on command line */
  43.         cprintf("\n\rInput filename for report\r\n");
  44.         gets(fname);
  45.     }
  46.     else{    /* command link provided file name */
  47.         strcpy(fname,argv[1]);
  48.     }
  49.     strcpy(f_noext_name,fname);
  50.     if((period_pos=strchr(f_noext_name,'.'))==NULL){    /* need to add default .exe file extension */
  51.         strcat(fname,".exe");    /* add the .exe extension */
  52.     }
  53.     else{
  54.         period_pos[0]=0;    /* kill the extension */
  55.     }
  56.     sprintf(temp,"Report on file %s",fname);
  57.     printf("\n\n%s",temp);    /* print header */
  58.     strset(temp,'=');    /* make a separation just as long as the header line */
  59.     printf("\n%s",temp);    /* print separation line */
  60.     getdate(&today);    /* get date of MAPOVL run */
  61.     gettime(&now);    /* get time of MAPOVL run */
  62.     printf("\n\nDATE:  %d/%d/%d\nTIME:  %02d:%02d:%02d",today.da_mon,today.da_day,
  63.         today.da_year,now.ti_hour,now.ti_min,now.ti_sec);    /* show date/time of run */
  64.     operate();
  65. }
  66.  
  67. void operate()
  68. {
  69.     int breakflag=0;
  70.  
  71.     if((fp=fopen(fname,"rb"))==NULL){    /* error opening file */
  72.         printf("\n\007Error opening file %s\n",fname);
  73.         exit(1);
  74.     }
  75.     setvbuf(fp,buff,_IOFBF,8192);    /* speed up i/o */
  76.     while(!feof(fp)){    /* split out .exe files until end of file or invalid .exe information */
  77.         breakflag=get_info();
  78.         if(breakflag)    /* no more files, or error */
  79.             break;
  80.         report();
  81.         filecount++;    /* increment count of overlay files */
  82.     }
  83.     if(ovl_largest>0){    /* only print if there is an overlay file */
  84.         printf("\n\nOverlay file %d always loads into the standard overlay area.",
  85.             which_ovl);
  86.     }
  87.     fclose(fp);
  88.  
  89.     printf("\n\nReport complete: %d overlay file%s\n",filecount-1,filecount!=2?"s":"");
  90. }
  91.  
  92. int get_info()
  93. {
  94.     int signature,ovl_number;
  95.     fread(header,27,1,fp);    /* read in first 27 .exe header bytes */
  96.     fseek(fp,-27L,SEEK_CUR);    /* position back to start of .exe file */
  97.  
  98.     signature=(header[0]=='M' && header[1]=='Z');    /* check for valid MZ .exe signature */
  99.     ovl_number=header[26];    /* get overlay number */
  100.     if(!signature || ovl_number!=filecount){    /* header bytes have invalid values */
  101.         return(1);    /* not a proper .exe file */
  102.     }
  103.     header[0]=header[1]=0;    /* force invalid signature if no more bytes to read */
  104.  
  105.     filelen=header[5];
  106.     filelen*=256L;
  107.     filelen+=header[4];    /* size of file in 512 byte pages */
  108.     filelen*=512L;    /* get length of .exe file to nearest 512 byte page */
  109.  
  110.     header_size=header[9];
  111.     header_size*=256;
  112.     header_size+=header[8];    /* size of header in paragraphs */
  113.     header_size*=16;    /* size of header in bytes */
  114.  
  115.     len_no_hdr=filelen-header_size;    /* size of file without any .exe header bytes (size of code) */
  116.  
  117.     relocation_count=header[7];
  118.     relocation_count*=256;
  119.     relocation_count+=header[6];    /* get number of relocation table items */
  120.  
  121.     relocation_bytes=(long)relocation_count*4L;    /* get byte count of relocation table items */
  122.  
  123.     fseek(fp,filelen,SEEK_CUR);    /* position to read info of next .exe file */
  124.  
  125.     return(0);    /* successful read of information */
  126. }
  127.  
  128. void report()
  129. {
  130.     if(!filecount){    /* root file */
  131.         printf("\n\nRoot file:  File size %6ld (%lx hex) bytes",len_no_hdr,len_no_hdr);
  132.         printf("\n    Relocation table %5d (%x hex) items, %6ld (%lx hex) bytes",
  133.             relocation_count,relocation_count,relocation_bytes,relocation_bytes);
  134.         printf("\n    Unaffected by /ZBn option");
  135.     }
  136.     else{    /* overlay file */
  137.         printf("\n\nOverlay file %2d:  File size %6ld (%lx hex) bytes",filecount,
  138.             len_no_hdr,len_no_hdr);
  139.         printf("\n    Relocation table %5d (%x hex) items, %6ld (%lx hex) bytes",
  140.             relocation_count,relocation_count,relocation_bytes,relocation_bytes);
  141.         printf("\n    /ZBn option requires 'n' of %d for no table reload",
  142.             relocation_count==0?1:1+(int)((relocation_bytes-1)/1024));
  143.         if(len_no_hdr>ovl_largest){    /* new largest overlay file */
  144.             ovl_largest=len_no_hdr;
  145.             which_ovl=filecount;
  146.         }
  147.     }
  148. }
  149.